package com.nbjtjc.safe.bean;

import cn.hutool.core.collection.CollectionUtil;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.service.sys.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @auther guqianbin
 * @date 2018/5/28 13:44
 **/
@Component
public class MyCache {
    private ConcurrentHashMap<Integer,SysUser> userMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer,EmgPlanPublic> emgPlanPublicMap=new ConcurrentHashMap<>();
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EmgPlanPublicMapper emgPlanPublicMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    private HashMap<Integer,SysDept> sysDeptHashMap=new HashMap<>();

//    @PostConstruct
    public void init(){
       List<SysUser> sysUsers= sysUserMapper.search(null,new Page());
       sysUsers.forEach(sysUser -> {
           if(!userMap.containsKey(sysUser.getUserId())){
               userMap.put(sysUser.getUserId(),sysUser);
           }
       });
    }

    private void initDeptData(){

       List<SysDept> sysDepts= sysDeptMapper.getTopCompany();
        sysDepts.forEach(sysDept -> {
            getSubDept(sysDept);
        });
    }

    private void getSubDept(SysDept sysDept) {
        List<SysDept> sysDepts = sysDeptMapper.findByParentId(sysDept.getId());
        if (CollectionUtil.isNotEmpty(sysDepts)) {
            sysDept.setSubDept(sysDepts);
            sysDepts.forEach(dept -> {
                getSubDept(dept);
            });
        }
        sysDeptHashMap.put(sysDept.getId(),sysDept);
    }
    public SysUser getUser(int userId){
        if(userMap.containsKey(userId)){
            return userMap.get(userId);
        }else{
            return new SysUser();
        }
    }
    public String getUserName(int userId){
        if(userMap.containsKey(userId)){
            return userMap.get(userId).getUsername();
        }else{
            return "";
        }
    }
    public String getUserName(String userId){
        int uId=Integer.parseInt(userId);
        return getUserName(uId);
    }

    public EmgPlanPublic getEmgPlanPublic(int planPublicId){
        return emgPlanPublicMap.get(planPublicId);
    }
}
